/*
 *    Document   : StReproducaoMaterial
 *    Created on : 04/03/2013, 16:32:52
 *    Author     : Fiúza <rbfiuza@gmail.com>
 */

Ext.define('FC.controller.StReproducaoMaterial', {
    extend: 'Ext.app.Controller',

    stores:['StReproducaoMaterial'],
    models:['StReproducaoMaterial'],
    views:['StReproducaoMaterialList'],
   
    init : function() {
        this.control({
   
            'stReproducaoMaterialList': {
                edit: this.editStReproducaoMaterial

            }
        });
    },
    
    editStReproducaoMaterial : function(editor, obj) {
        //check if record is dirty
        if(obj.record.dirty){
            //check if the record is valid   
//            console.log(obj.record.validate());
            if(obj.record.validate().isValid()){
                //Make your Ajax request to sync data
                mode = (obj.record.get('userId') === "") ? 'insert': 'update';
                this.syncData(obj, mode);
            }
        }
    },
            
    //Sync data with the server 
    syncData : function(obj, mode) {
        Ext.Ajax.request({
            url: 'atualizaStReproducaoMaterial',
            params: {
                store_id: 1,
                action: mode,
                rowIndex: obj.rowIdx,
                recordInfo: Ext.encode(this.getStReproducaoMaterialStore().getAt(obj.rowIdx).data)
            },
            scope:this,
            //method to call when the request is successful
            success: this.onSaveSuccess,
            //method to call when the request is a failure
            failure: this.onSaveFailure
        });
    },

    onSaveFailure: function(err){
        //Alert the user about communication error
        Ext.MessageBox.alert('Status', 'Error occured during update Diaria');
    },
    
    onSaveSuccess: function(response,opts){
        //Remove dirty
        var ajaxResponse = Ext.decode(response.responseText);
        if(ajaxResponse.success){
            //if we are doing an insert then get the new customerId 
            //and update the store record
            if(opts.params.action === 'insert'){
                mcTonnerId = ajaxResponse.mcTonnerId;
                this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('stReproducaoMaterialId',mcTonnerId);
            }

//            this.getDiariasStore().getAt(opts.params.rowIndex).set('id',ajaxResponse.diarias.polos);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('dscStReproducaoMaterial',ajaxResponse.streproducaomaterial.dscStReproducaoMaterial);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntAlunos',ajaxResponse.streproducaomaterial.qntAlunos);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntTutores',ajaxResponse.streproducaomaterial.qntTutores);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntPolos',ajaxResponse.streproducaomaterial.qntPolos);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntProf',ajaxResponse.streproducaomaterial.qntProf);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntExemplarDisc',ajaxResponse.streproducaomaterial.qntExemplarDisc);            
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntDisciplinas',ajaxResponse.streproducaomaterial.qntDisciplinas);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('qntTotal',ajaxResponse.streproducaomaterial.qntTotal);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('vlrUnit',ajaxResponse.streproducaomaterial.vlrUnit);
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('vlrFinal',ajaxResponse.streproducaomaterial.vlrFinal);            
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).set('indicacoesParametros',ajaxResponse.streproducaomaterial.indicacoesParametros);            
            this.getStReproducaoMaterialStore().getAt(opts.params.rowIndex).commit();
//            this.getDiariasStore().save();

        }
        else {
            Ext.MessageBox.alert('Status', 'Error occured during update streproducaomaterial');
        }
    
    }
});